package com.farmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farmer.mapper.AdminMapper;
import com.farmer.pojo.Admin;
import com.farmer.pojo.Notice;
import com.farmer.service.NoticeService;
import com.farmer.mapper.NoticeMapper;
import com.farmer.utils.JwtHelper;
import com.farmer.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

/**
* @author 86183
* @description 针对表【notice(公告信息表)】的数据库操作Service实现
* @createDate 2024-05-12 11:43:00
*/
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>
    implements NoticeService{
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private JwtHelper jwtHelper;
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Result add(String token, Notice notice) {
        int id = jwtHelper.getUserId(token).intValue();
        Admin admin = adminMapper.selectById(id);
        notice.setUser(admin.getUsername());
        notice.setTime(DateFormat.getDateInstance().format(new Date()));
        noticeMapper.insert(notice);
        return Result.ok(null);
    }

    @Override
    public Result delete(Integer id) {
        noticeMapper.deleteById(id);
        return null;
    }

    @Override
    public Result update(Notice notice) {
        noticeMapper.updateById(notice);
        return Result.ok(null);
    }


    @Override
    public Result getById(Integer id) {
        Notice notice = noticeMapper.selectById(id);
        if (notice!=null){
            return Result.build(notice,111,"查询成功");
        }else{
            return Result.build(null,101,"查询失败");
        }

    }

    @Override
    public Result getAll() {
        List<Notice> notices = noticeMapper.selectList(null);
        Result result = new Result<>();
        if (notices != null && !notices.isEmpty()) {
            return Result.build(notices, 111, "查询成功");
        } else {
            return Result.build(null, 101, "查询失败");
        }
    }

    @Override
    public Result getByTitleLike(String title){
        LambdaQueryWrapper<Notice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Notice::getTitle,title);
        List<Notice> notices = noticeMapper.selectList(lambdaQueryWrapper);
        if (notices!=null&&!notices.isEmpty()){
            return Result.build(notices,111,"查询成功");
        }else{
            return Result.build(null,101,"未找到匹配信息");
        }
    }

    @Override
    public Result deleteBatch(List<Integer> ids){
        for (Integer id :ids) {
            noticeMapper.deleteById(id);
        }
        return Result.ok(null);
    }

    /*
     * 分页查询*/
    @Override
    public Result page(Integer pageNum, Integer pageSize){
        // 创建分页构造对象
        Page<Notice> page = new Page<>(pageNum, pageSize);
        // 执行分页查询
        IPage<Notice> result = noticeMapper.selectPage(page, null);
        // 封装到统一响应体Result中
        return Result.ok(result);
    }
}




